﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CandyMatchedConditions : MonoBehaviour
{
    public static CandyMatchedConditions instance;
    private GameManager manager;
    void Awake()
    {
        instance = this;
        manager = GameManager.getInstance();
    }

    private string typeSpecial;
    public List<GameObject> candy_list_matched = new List<GameObject>();
    public void StarAndSimple(GameObject star, GameObject simple)
    {

        foreach (GameObject cs in CandyDemo.instance.level.GetCandy())
        {
			if (cs.GetComponent<CandyController>().Candy_Color == simple.GetComponent<CandyController>().Candy_Color)
            {
                candy_list_matched.Add(cs);
                CandyEffect.instance.candy_electric.Add(cs);
            }
        }
		manager.gamestate=GameState.destroy;
        CandyEffect.instance.AssignBurstUniversalObject(star.transform.position, false);
        candy_list_matched.Add(star);
		star.GetComponent<CandyController>().Type = "simple";
        CandyDemo.instance.UIscore.AddScore(40 * candy_list_matched.Count);
        Match.instance.SetMatchedCandy(candy_list_matched, null, "simple", false);
        manager.IsMatch = true;
        SoundCon.instance.PlaySound(9);
    }
    public void StarAndStar()
    {
        foreach (GameObject cs in CandyDemo.instance.level.GetCandy())
        {
            candy_list_matched.Add(cs);
        }
        CandyDemo.instance.UIscore.AddScore(40 * candy_list_matched.Count);
        Match.instance.SetMatchedCandy(candy_list_matched, null, "", false);
        manager.IsMatch = true;
		manager.gamestate=GameState.destroy;
        CandyDrop.instance.DroppingCandy();
    }
    public void StarWithVH(GameObject star, GameObject vh)
    {
        for (int i = 0; i < CandyDemo.instance.level.GetCandy().Length; i++)
        {
			if (CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().Candy_Color == vh.GetComponent<CandyController>().Candy_Color)
            {
                CandyEffect.instance.candy_electric.Add(CandyDemo.instance.level.GetCandy()[i]);
                candy_list_matched.Add(CandyDemo.instance.level.GetCandy()[i]);
            }
        }
		manager.gamestate=GameState.destroy;
		star.GetComponent<CandyController>().IsMatched = true;
		star.GetComponent<CandyController>().Type = "simple";
        CandyEffect.instance.AssignBurstUniversalObject(star.transform.position, true);
        manager.IsMatch = true;
    }
    public void SpecialWithSimple(GameObject obj) // SpecialMatch
	{
        hit = Physics2D.OverlapCircleAll(obj.transform.position, 1.5f);
        for (int j = 0; j < hit.Length; j++)
        {
            if (hit[j].gameObject.tag == "Candy")
            {
                hit[j].gameObject.GetComponent<CandyController>().IsMatched = true;
                CandyDemo.instance.UIscore.AddScore(40);
            }
        }
		manager.gamestate=GameState.destroy;
		manager.IsMatch = true;
        SoundCon.instance.PlaySound(8);
    }
    public void SpecialAndSpecial(GameObject target_)
    {
        hit = Physics2D.OverlapCircleAll(target_.transform.position, 3);
        for (int j = 0; j < hit.Length; j++)
        {
            if (hit[j].gameObject.tag == "Candy")
            {
                hit[j].gameObject.GetComponent<CandyController>().IsMatched = true;
                CandyDemo.instance.UIscore.AddScore(40);
            }
        }
		manager.gamestate=GameState.destroy;
        manager.IsMatch = true;
        SoundCon.instance.PlaySound(8);
    }
    public void SpecialAndHorizontal(GameObject click_, GameObject target_)
    {
        Match.instance.CreateSlideCandy(click_, "vertical");
        Match.instance.CreateSlideCandy(click_, "horizontal");
		Match.instance.CreateSlideCandy(click_, click_.GetComponent<CandyController>().GetNeighbor("right"), click_.GetComponent<CandyController>().GetNeighbor("left"), click_.GetComponent<CandyController>().GetNeighbor("up"),
		                                click_.GetComponent<CandyController>().GetNeighbor("down"));
		CandyNeighbor.instance.clicked_candy_.GetComponent<CandyController>().IsMatched = CandyNeighbor.instance.target_candy.GetComponent<CandyController>().IsMatched = true;
		CandyNeighbor.instance.clicked_candy_.GetComponent<CandyController>().Type = CandyNeighbor.instance.target_candy.GetComponent<CandyController>().Type = "simple";
        manager.IsMatch = true;
    }
    public void VerticalORHorizontal(GameObject click_, GameObject target_)
    {
        Match.instance.CreateSlideCandy(target_, "vertical");
        Match.instance.CreateSlideCandy(target_, "horizontal");
		click_.GetComponent<CandyController>().IsMatched = true;
		target_.GetComponent<CandyController>().IsMatched = true;
        CandyEffect.instance.CreateEffect(target_, "vertical");
        CandyEffect.instance.CreateEffect(target_, "horizontal");
		target_.GetComponent<CandyController>().Type = "simple";
		click_.GetComponent<CandyController>().Type = "simple";
    }
    public void StarAndSpecial(GameObject star, GameObject special)
    {
        foreach (GameObject cs in CandyDemo.instance.level.GetCandy())
        {
			if (cs.GetComponent<CandyController>().Candy_Color == special.GetComponent<CandyController>().Candy_Color)
            {
                candy_list_matched.Add(cs);
				CandyEffect.instance.candy_electric.Add(cs);
            }
        }
        candy_list_matched.Add(special);
        candy_list_matched.Add(star);
		star.GetComponent<CandyController>().Type = "simple";
		CandyDemo.instance.UIscore.AddScore(40 * candy_list_matched.Count);
		Match.instance.SetMatchedCandy(candy_list_matched, special, "star", false);
		manager.IsMatch = true;
		CandyEffect.instance.AssignBurstUniversalObject(star.transform.position,false);
    }
    private Collider2D[] hit;
    public void SwitchStar(GameObject star, GameObject other)
    {
		switch (other.GetComponent<CandyController>().Type)
        {
            case "simple": StarAndSimple(star, other); break;
            case "special": StarAndSpecial(star, other); break;
            case "vertical": StarWithVH(star, other); break;
            case "horizontal": StarWithVH(star, other); break;
            case "star": StarAndStar(); break;
        }
    }
}
